/**
 * 简化的PDF导出功能
 * 使用浏览器原生打印功能，支持Tailwind CSS样式
 */

export const useSimplePdfExport = () => {
  /**
   * 使用浏览器打印功能导出PDF
   * @param element 要导出的DOM元素
   * @param filename 文件名
   */
  const exportToPdfByPrint = (element: HTMLElement, filename: string = 'resume.pdf') => {
    try {
      // 创建新窗口
      const printWindow = window.open('', '_blank')
      if (!printWindow) {
        throw new Error('无法打开打印窗口，请检查浏览器弹窗设置')
      }

      // 获取元素内容
      const elementContent = element.innerHTML
      
      // 创建打印页面HTML，包含Tailwind CSS样式
      const printContent = `
        <!DOCTYPE html>
        <html>
        <head>
          <meta charset="utf-8">
          <title>${filename}</title>
          <script src="https://cdn.tailwindcss.com"></script>
          <style>
            /* 自定义样式覆盖 */
            body {
              font-family: 'Microsoft YaHei', 'PingFang SC', 'Hiragino Sans GB', sans-serif;
              line-height: 1.6;
              color: #1f2937;
              margin: 0;
              padding: 0;
              background: white;
            }
            
            /* 确保打印时样式正确 */
            @media print {
              body {
                margin: 0;
                padding: 0;
                background: white;
              }
              
              /* 移除阴影和边框 */
              .shadow-lg {
                box-shadow: none !important;
              }
              
              /* 确保最大宽度在打印时占满整个页面 */
              .max-w-4xl {
                max-width: 100% !important;
                width: 100% !important;
              }
              
              /* 调整内边距，减少边距以占满页面 */
              .p-10 {
                padding: 15px !important;
              }
              
              /* 确保容器占满整个页面宽度 */
              .mx-auto {
                margin-left: 0 !important;
                margin-right: 0 !important;
              }
              
              /* 确保颜色在打印时显示 */
              .text-blue-500 {
                color: #3b82f6 !important;
              }
              
              .text-gray-800 {
                color: #1f2937 !important;
              }
              
              .text-gray-600 {
                color: #4b5563 !important;
              }
              
              .text-gray-500 {
                color: #6b7280 !important;
              }
              
              .border-blue-500 {
                border-color: #3b82f6 !important;
              }
              
              .border-gray-200 {
                border-color: #e5e7eb !important;
              }
              
              /* 确保字体粗细正确 */
              .font-bold {
                font-weight: 700 !important;
              }
              
              .font-semibold {
                font-weight: 600 !important;
              }
              
              .font-medium {
                font-weight: 500 !important;
              }
              
              /* 确保间距正确 */
              .mb-8 {
                margin-bottom: 2rem !important;
              }
              
              .mb-5 {
                margin-bottom: 1.25rem !important;
              }
              
              .mb-3 {
                margin-bottom: 0.75rem !important;
              }
              
              .mb-2 {
                margin-bottom: 0.5rem !important;
              }
              
              .pb-5 {
                padding-bottom: 1.25rem !important;
              }
              
              .pb-2 {
                padding-bottom: 0.5rem !important;
              }
              
              .gap-3 {
                gap: 0.75rem !important;
              }
              
              .gap-1 {
                gap: 0.25rem !important;
              }
              
              /* 确保flexbox布局正确 */
              .flex {
                display: flex !important;
              }
              
              .flex-col {
                flex-direction: column !important;
              }
              
              .items-start {
                align-items: flex-start !important;
              }
              
              .items-center {
                align-items: center !important;
              }
              
              .justify-between {
                justify-content: space-between !important;
              }
              
              .flex-1 {
                flex: 1 1 0% !important;
              }
              
              .ml-auto {
                margin-left: auto !important;
              }
              
              /* 确保文字大小正确 */
              .text-3xl {
                font-size: 1.875rem !important;
                line-height: 2.25rem !important;
              }
              
              .text-lg {
                font-size: 1.125rem !important;
                line-height: 1.75rem !important;
              }
              
              .text-base {
                font-size: 1rem !important;
                line-height: 1.5rem !important;
              }
              
              .text-sm {
                font-size: 0.875rem !important;
                line-height: 1.25rem !important;
              }
              
              /* 确保边框正确 */
              .border-b-2 {
                border-bottom-width: 2px !important;
              }
              
              .border-b {
                border-bottom-width: 1px !important;
              }
              
              /* 确保leading正确 */
              .leading-relaxed {
                line-height: 1.625 !important;
              }
              
              /* 确保margin和padding重置 */
              .mx-auto {
                margin-left: auto !important;
                margin-right: auto !important;
              }
              
              .m-0 {
                margin: 0 !important;
              }
              
              .p-0 {
                padding: 0 !important;
              }
            }
          </style>
        </head>
        <body>
          <div class="max-w-4xl mx-auto bg-white p-10 shadow-lg font-sans leading-relaxed">
            ${elementContent}
          </div>
          <script>
            window.onload = function() {
              // 等待Tailwind CSS加载完成
              setTimeout(() => {
                window.print();
                window.onafterprint = function() {
                  window.close();
                };
              }, 500);
            };
          </script>
        </body>
        </html>
      `

      // 写入内容到新窗口
      printWindow.document.write(printContent)
      printWindow.document.close()

      return true
    } catch (error) {
      console.error('打印导出失败:', error)
      throw error
    }
  }

  /**
   * 导出简历为PDF（使用打印功能）
   * @param element 要导出的DOM元素
   * @param filename 文件名
   */
  const exportResumeToPdf = async (element: HTMLElement, filename: string = 'resume.pdf') => {
    try {
      return exportToPdfByPrint(element, filename)
    } catch (error) {
      console.error('简历PDF导出失败:', error)
      throw error
    }
  }

  return {
    exportToPdfByPrint,
    exportResumeToPdf
  }
}
